package com.kbbywyclient.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.data.entity.ResultInfo;
import com.kbbywyclient.ui.adapter.base.BaseLvAdapter;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ViewUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Description:
 * Created by FHW
 * on 2017/4/10
 */

public class MatchTableAdapter extends BaseLvAdapter<ResultInfo> {


    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_education)
        TextView tv_education;
        @BindView(R.id.tv_marriage)
        TextView tv_marriage;
        @BindView(R.id.tv_occupation)
        TextView tv_occupation;
        @BindView(R.id.tv_income)
        TextView tv_income;
        @BindView(R.id.tv_credit)
        TextView tv_credit;
        @BindView(R.id.iv_buy)
        ImageView iv_buy;
        @BindView(R.id.tv_product_name)
        TextView tv_product_name;
        @BindView(R.id.tv_time)
        TextView tv_time;

        @BindView(R.id.iv_sex)
        ImageView iv_sex;
        @BindView(R.id.tv_loan)
        TextView tv_loan;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public MatchTableAdapter(Context context, int itemLayout, List<ResultInfo> list) {
        super(context, itemLayout, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (getItemViewType(position) == TYPE_NO_DATA) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.layout_data_error, null);
            }
        }else{
            ResultInfo resultInfo = getItem(position);

            ViewHolder holder = null;
            if (convertView == null) {
                convertView = getItemView();
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            ViewUtils.setText(holder.tv_name, resultInfo.getUserName());

            ViewUtils.setText(holder.tv_loan,resultInfo.getLoansMoney());

            if (StringUtil.isNotEmpty(resultInfo.getHighestEducation())) {
                holder.tv_education.setVisibility(View.VISIBLE);
                ViewUtils.setText(holder.tv_education, resultInfo.getHighestEducation());
            }
            if (StringUtil.isNotEmpty(resultInfo.getMarriage())) {
                holder.tv_marriage.setVisibility(View.VISIBLE);
                ViewUtils.setText(holder.tv_marriage, resultInfo.getMarriage());
            }
            if (StringUtil.isNotEmpty(resultInfo.getWorkTypeList())) {
                holder.tv_occupation.setVisibility(View.VISIBLE);
                ViewUtils.setText(holder.tv_occupation, resultInfo.getWorkTypeList());
            }
            if (StringUtil.isNotEmpty(resultInfo.getIncomeRange())) {
                holder.tv_income.setVisibility(View.VISIBLE);
                ViewUtils.setText(holder.tv_income, resultInfo.getIncomeRange());
            }
            if (StringUtil.isNotEmpty(resultInfo.getCreditCard())) {
                holder.tv_credit.setVisibility(View.VISIBLE);
                ViewUtils.setText(holder.tv_credit, resultInfo.getCreditCard());
            }

            ViewUtils.setText(holder.tv_product_name, resultInfo.getSalesCountText());
            ViewUtils.setText(holder.tv_time, resultInfo.getApplyDateTime());

            holder.iv_buy.setOnClickListener(v -> {
                EventBus.getDefault().post(new Event.LoansTableBuy(position));
            });
        }

        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if(mList==null||mList.size()==0){
            return TYPE_NO_DATA;
        }
        return TYPE_NORMAL;
    }

    @Override
    public int getCount() {
        return mList==null||mList.size()==0?1:mList.size();
    }
}
